#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <TlHelp32.h>

#define ERROR_EXIT_CODE -1

//macros for extracting differet parts from the server 32bit UID

#define SID_OS_MINOR_VERSION(sid) ((sid>>18)&7)
#define SID_OS_MAJOR_VERSION(sid) ((sid>>21)&7)
#define SID_SP_MINOR_VERSION(sid) ((sid>>24)&7)
#define SID_SP_MAJOR_VERSION(sid) ((sid>>27)&7)
#define SID_PRODUCT_TYPE(sid) ((sid>>30)&3)
#define SID_MAC_THIRD_FIRST_HALF_BYTE(sid) ((sid) & 3)
#define SID_MAC_SECOND_BYTE(sid) ((sid>>2) & 0xFF)
#define SID_MAC_FIRST_BYTE(sid) ((sid>>10) & 0xFF)

/*
 * this function trying to print some textual data out of OS ans SP values
 */
void printOsInfo(size_t productType,size_t osMajorVersion,size_t osMinorVersion)
{
	if (osMajorVersion == 6 && osMinorVersion == 1)
	{
		if (productType == VER_NT_WORKSTATION)
		{
			printf("(Windows 7)");
		}
		else
		{
			printf("(Windows Server 2008 R2)");
		}

		return;
	}

	if (osMajorVersion == 6 && osMinorVersion == 0)
	{
		if (productType == VER_NT_WORKSTATION)
		{
			printf("(Windows Vista)");
		}
		else
		{
			printf("(Windows Server 2008)");
		}

		return;
	}

	if (osMajorVersion == 5 && osMinorVersion == 2)
	{
		printf("(Windows: Server 2003 R2 | Server 2003 | Home Server | XP Professional x64 Edition)");
		return;
	}
	
	if (osMajorVersion == 5 && osMinorVersion == 1)
	{
		printf("(Windows XP)");
		return;
	}

	if (osMajorVersion == 5 && osMinorVersion == 1)
	{
		printf("(Windows 2000)");
		return;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	int sid;
	size_t productType;
	if (argc != 2)
	{
		_tprintf(_T("usage: %s <SID>\n"),argv[0]);
		return ERROR_EXIT_CODE;
	}

	sid = _wtoi(argv[1]);

	if (sid == 0)
	{
		printf("Error while reading sid\n");
		return ERROR_EXIT_CODE;
	}

	productType = SID_PRODUCT_TYPE(sid);

	printf("Server's product type: %d",productType);
	if (productType == VER_NT_WORKSTATION)
	{
		printf(" (Workstation)\n");
	}
	else if (productType == VER_NT_SERVER)
	{
		printf(" (Server)\n");
	}
	else if (productType == VER_NT_DOMAIN_CONTROLLER)
	{
		printf(" (Domain Controller)\n");
	}
	else
	{
		printf("\n");
	}

	//print extracted data to the screen

	printf("Server's OS Version : %d.%d ",SID_OS_MAJOR_VERSION(sid),SID_OS_MINOR_VERSION(sid));
	printOsInfo(productType,SID_OS_MAJOR_VERSION(sid),SID_OS_MINOR_VERSION(sid));
	printf("\n");
	printf("Server's SP Version : %d.%d\n",SID_SP_MAJOR_VERSION(sid),SID_SP_MINOR_VERSION(sid));
	printf("Server's MAC address: \?\?-\?\?-\?\?-\?%X-%02X-%02X\n",SID_MAC_THIRD_FIRST_HALF_BYTE(sid),SID_MAC_SECOND_BYTE(sid),SID_MAC_FIRST_BYTE(sid));
	return 0;

}

